package com.iptech.gxt.theme.blue.client.tabs;


import java.util.HashMap;

import org.apache.commons.collections.map.HashedMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.dom.XElement;
import com.sencha.gxt.core.client.util.Point;
import com.sencha.gxt.theme.blue.client.tabs.BlueTabPanelAppearance;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.tips.ToolTipConfig;

public class ApplicationTabPanelAppearance extends BlueTabPanelAppearance {

	public interface ApplicationTabPanelStyle extends BlueTabPanelStyle {
		String appTabStripWrap();
	}

	public interface ApplicationTabPanelResource extends BlueTabPanelResources {
		
		public static final ApplicationTabPanelResource INSTANCE = GWT.<ApplicationTabPanelResource> create(ApplicationTabPanelResource.class);
		
		@Source({ "com/sencha/gxt/theme/base/client/tabs/TabPanel.css",
				"com/sencha/gxt/theme/blue/client/tabs/BlueTabPanel.css",
				"ApplicationTabPanel.css" })
		ApplicationTabPanelStyle style();	
		
		ImageResource defaultAppTabItemIcon();
	}

	public interface ApplicationTabPanelTemplate extends Template {

		@XTemplate(source = "com/sencha/gxt/theme/base/client/tabs/TabPanel.html")
		SafeHtml render(TabPanelStyle style);

		@XTemplate(source = "ApplicationTabPanel.html")
		SafeHtml render(ApplicationTabPanelStyle style);
	}

	private final ApplicationTabPanelStyle style;
	private final ApplicationTabPanelTemplate template;

	public ApplicationTabPanelAppearance() {
		this(GWT.<ApplicationTabPanelResource> create(ApplicationTabPanelResource.class),
				GWT.<ApplicationTabPanelTemplate> create(ApplicationTabPanelTemplate.class),
				GWT.<ItemTemplate> create(ItemTemplate.class));
	}

	public ApplicationTabPanelAppearance(ApplicationTabPanelResource resources,
			ApplicationTabPanelTemplate template, ItemTemplate itemTemplate) {
		super(resources, template, itemTemplate);
		this.style = resources.style();
		this.template = template;

	}

	@Override
	public void render(SafeHtmlBuilder builder) {
		builder.append(template.render(style));
	}

	public XElement getAppTabStripWrap(XElement parent) {
		return parent.selectNode("." + style.appTabStripWrap());
	}

	@Override
	public XElement getStrip(XElement parent) {
		return getStripWrap(parent).selectNode("."+style.tabStrip());
	}
	
	@Override
	public XElement getStripEdge(XElement parent) {
		return getStripWrap(parent).selectNode("."+style.tabEdge());
	}
	
	public XElement getChildStrip(XElement parent) {
		return parent.selectNode("." + style.tabStrip());
	}


	public void move(XElement parent, XElement item, int index) {
		getStrip(parent).insertChild(item, index);
	}
	
	@Override
	public void updateItem(XElement item, TabItemConfig config) {
		super.updateItem(item, config);
		if(!config.isClosable()){
			item.removeClassName(style.tabStripClosable());
		}else{
			item.addClassName(style.tabStripClosable());
		}
	}
	
	public XElement getTabRight(XElement parent){
		return parent.selectNode("." + style.tabRight());
	}
	
	public XElement getTabStripText(XElement parent){
		return parent.selectNode("." + style.tabStripText());
	}
	
	
	private HashMap<Widget, XElement> appTabItemWidgets = new HashMap<Widget, XElement>();
	
	private static class AppTabItemComponent extends Component{
		public AppTabItemComponent(Element element){
			setElement(element);
		}
	};
	
	public void onAppItemMouseOver(XElement parent, Event event) {
		XElement target = event.getEventTarget().cast();
		XElement appTabStripWrap = getAppTabStripWrap(parent);
		
		if(appTabStripWrap.isOrHasChild(target)){
			Element item = findItem(event.getEventTarget().<Element> cast());
			if (item != null) {
				AppTabItemComponent itemWidget = new AppTabItemComponent(item);
				appTabItemWidgets.put(itemWidget, item.<XElement> cast());
				
				ToolTipConfig toolTipConfig = new ToolTipConfig();
				toolTipConfig.setHideDelay(0);
				toolTipConfig.setShowDelay(200);
				toolTipConfig.setDismissDelay(2000);
				toolTipConfig.setBodyText(item.<XElement> cast().selectNode("." + style.tabStripText()).getInnerHTML());
				itemWidget.setToolTipConfig(toolTipConfig);
			    
				Point point = (item.<XElement> cast()).getXY();
			    point.setX(point.getX() + 30);
			    point.setY(point.getY() + 10);
			    itemWidget.getToolTip().showAt(point);
			}
		}
	}
	
	public boolean isScrolling(XElement parent){
		XElement tabHeader = getBar(parent);
		if(tabHeader.hasClassName(style.tabScrolling())){
			return true;
		}
		return false;
	}


}
